package com.edais.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.edais.domain.UserVouchers;
import com.edais.domain.VouchersRule;
import com.edais.persistence.UserVouchersMapper;


@Repository

public class UserVouchersDao {
	@Autowired
	private UserVouchersMapper userVouchersMapper;
	
	/**
	 * 查询我的代金券
	 * @param paramsMap
	 * @return
	 */
	public List<UserVouchers> queryVochersByUserId(Map<String,Object> paramsMap){
		Object  vsFlag=paramsMap.get("vsFlag");//加排序和不加排序版本的区别标识：0-老版本，1-新版本
		if (null==vsFlag||"".equals(vsFlag)||"0".equals(vsFlag)) {	
			return this.userVouchersMapper.queryVochersByUserId(paramsMap);
		}else{
			//add by gongyl 20161207
			Object  sortType=paramsMap.get("sortType");//sortType=1，按有效期限升序：即将失效的页面上方（进入页面默认顺序），sortType=2：满xxx可用，金额小的在页面上方，sortType=3，按代金券面值降序：金额大的在页面上方
			String[] colArr={"uv.enddate asc","uv.investment_amount asc","uv.voucher_amount desc"};//未排序字段
			String[] orderCol={"orderCol1","orderCol2","orderCol3"};
			List<String> sortCorList=new ArrayList<String>();//已排序字段
			if (null==sortType||"".equals(sortType)||"1".equals(sortType)) {
				sortCorList.add(0,colArr[0]);//有效期限
				sortCorList.add(1,colArr[1]);//投资金额
				sortCorList.add(2,colArr[2]);//代金券面值
			}else if ("2".equals(sortType)) {
				sortCorList.add(0,colArr[1]);
				sortCorList.add(1,colArr[0]);
				sortCorList.add(2,colArr[2]);
			}else if ("3".equals(sortType)) {
				sortCorList.add(0,colArr[2]);
				sortCorList.add(1,colArr[0]);
				sortCorList.add(2,colArr[1]);
			}
			for (int i = 0; i < orderCol.length; i++) {
				paramsMap.put(orderCol[i], sortCorList.get(i));
			}
			List<UserVouchers> list=this.userVouchersMapper.queryVochersByUserIdWithSort(paramsMap);
			return list;
		}
	}
	
	/**
	 * 查询我的代金券总数
	 * @param paramsMap
	 * @return
	 */
	public int queryVochersByUserIdCount(Map<String,Object> paramsMap){
		Object  vsFlag=paramsMap.get("vsFlag");//加排序和不加排序版本的区别标识：0-老版本，1-新版本
		if (null==vsFlag||"".equals(vsFlag)||"0".equals(vsFlag)) {	
			return this.userVouchersMapper.queryVochersByUserIdCount(paramsMap);
		}else{
			return this.userVouchersMapper.queryVochersByUserIdWithSortCount(paramsMap);
		}
	}
	
	/**
	 * 根据ID查询代金券规则
	 * @param paramsMap
	 * @return
	 */
	public VouchersRule queryVouchersRuleById(Map<String,Object> paramsMap){
		return this.userVouchersMapper.queryVouchersRuleById(paramsMap);
	}
	
	/**
	 * 根据ID查询用户代金券
	 * @param paramsMap
	 * @return
	 */
	public UserVouchers queryUserVochersById(Map<String,Object> paramsMap){
		return this.userVouchersMapper.queryUserVochersById(paramsMap);
	}
	
	/**
	 * 根据参数查询代金券
	 * @param paramsMap
	 * @return
	 */
	public List<UserVouchers> queryVouchersByParams(Map<String,Object> paramsMap){
		return this.userVouchersMapper.queryVouchersByParams(paramsMap);
	}
	
	public int queryVouchersByParamsCount(Map<String,Object> paramsMap){
		return this.userVouchersMapper.queryVouchersByParamsCount(paramsMap);
	}
	
	/**
	 * 添加代金券
	 * @param userVouchers
	 */
	public void addUserVouchers(UserVouchers userVouchers){
		userVouchersMapper.addUserVouchers(userVouchers);
	}
}
